import tokenUtil from "@/core/util/tokenUtil.js";
import { mapGetters } from "vuex";
import Vue from "vue";
import apiUtil from "@/core/util/apiUtil.js";
/**
 * 按钮权限
 */
export default {
  install(Vue) {
    Vue.directive('permission', {
      inserted(el, bindings) {      
          //el 是当前对象的所有值 ，bindings 包含了自定义属性的参数
          const CURRENT_HTTP_SESSION = "Current";    
          let httpSession = window.localStorage.getItem(CURRENT_HTTP_SESSION);
          let needCheckPermission = bindings.value.action;//判断标签中是否需要授权按钮
          let effect = bindings.value.effect;//禁用方式disabled/display
          if(null != httpSession && '' != httpSession){
            const permissions = JSON.parse(httpSession);
            const oauthAdminSysEntities = permissions.oauthAdminSysEntities;
            if(null != oauthAdminSysEntities && JSON.parse(oauthAdminSysEntities).length > 0){
              //过滤前端管理员权限按钮
              return;
            }
            if(null != permissions){
              let permissionsList = permissions.oauthFunctionInfoMethodMap;
              let item = (needCheckPermission in permissionsList)   
              if (!item) {
                el.setAttribute('disabled', 'true')  
                //注意 如果用隐藏方式则要删除父节点 采用disabled无需
                // el.style.display = 'none';
                // setTimeout(() => {
                //   el.parentNode.removeChild(el)
                // }, 0)
                if (effect === 'disabled') {//disabled
                  el.disabled = true
                  el.classList.add('is-disabled')
                } else {//display
                  el.style.display = 'none';
                  setTimeout(() => {
                    el.parentNode.removeChild(el)
                  }, 0)
                }
              }
            }
          }
        },
      }
    );
  }
}